package com.example.a2212park


import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.example.a2212park.databinding.ActivitySplashBinding
import com.example.common.view.adapter.ImageBannerAdapter
import com.example.common.view.view.BaseMvvmActivity
import com.example.db.entity.BannerEntity
import com.example.image.glide.GlideImgLoader
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
//首页倒计时页面
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_splash
    private var bannerData= mutableListOf<BannerEntity>()
    private lateinit var bannerAdater:ImageBannerAdapter
    private lateinit var mCountTimer:CountDownTimer
    override fun initView() {
        var isFisert=MMKV.defaultMMKV().decodeBool("isFisert",true)
        if (isFisert){
            MMKV.defaultMMKV().encode("isFisert",false)
            showAppBanner()
        }else{
            showAppAdv()
        }
    }
    //展示主图
    private fun showAppBanner() {
        //View.VISIBLE表示显示UI
        //View.GONE表示既不显示UI也不占据UI大小和位置
        //View.INVISIBLE表示不显示UI但可以占据UI大小和位置
        mDataBinding.clAppBanner.visibility=View.VISIBLE
        mDataBinding.clAppAdv.visibility=View.GONE
        bannerData.run{
            add(BannerEntity(0,R.drawable.ic_guang4,""))
            add(BannerEntity(1,R.drawable.ic_guang2,""))
            add(BannerEntity(2,R.drawable.ic_guang3,""))
        }
        bannerAdater=ImageBannerAdapter(bannerData)
        mDataBinding.appBanner.run {
            setAdapter(bannerAdater)
            //关联生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器
            indicator=CircleIndicator(this@SplashActivity)
            //设置页面切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面监听器
            addOnPageChangeListener(object :OnPageChangeListener{
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    //判断Banner是否到最后一页
                    if(position ==bannerData.size-1){
                        mDataBinding.tvStart.visibility=View.VISIBLE
                    }else{
                        mDataBinding.tvStart.visibility=View.GONE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {

                }
            })
        }
        //点击进入
        mDataBinding.tvStart.setOnClickListener {
            jumMainPage()
        }
    }
    //展示广告
    private fun showAppAdv() {
        mDataBinding.clAppBanner.visibility=View.GONE
        mDataBinding.clAppAdv.visibility=View.VISIBLE
        //显示广告图片
        mDataBinding.ivAdv.run {
            GlideImgLoader.loadResImage(this, R.drawable.ic_guang1,this)
        }
        //5秒倒计时
        mCountTimer=object :CountDownTimer(5000,1000){
            override fun onTick(millisUntilFinished: Long) {
                mDataBinding.tvAdvSkip.text="${millisUntilFinished/1000}S跳过"
            }

            override fun onFinish() {
                this@SplashActivity.jumMainPage()
            }

        }
        //启动倒计时
        mCountTimer.start()
        mDataBinding.tvAdvSkip.setOnClickListener {
            jumMainPage()
            onDestroy()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        mCountTimer?.cancel()
    }

    private fun jumMainPage() {
        ARouter.getInstance().build("/main/MainActivity").navigation()
        //startActivity(Intent(this@SplashActivity,MainActivity::class.java))
    }

}